package com.haixiaoke.smartLock.youdian;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.haixiaoke.common.exception.ServiceException;
import com.haixiaoke.common.utils.RandomNoUtils;
import com.haixiaoke.common.utils.StringUtils;
import com.haixiaoke.domain.SmartLock;
import com.haixiaoke.domain.SmartLockCard;
import com.haixiaoke.domain.SmartLockFace;
import com.haixiaoke.domain.SmartLockPwd;
import com.haixiaoke.service.impl.SmartLockCardServiceImpl;
import com.haixiaoke.service.impl.SmartLockFaceServiceImpl;
import com.haixiaoke.service.impl.SmartLockPwdServiceImpl;
import com.haixiaoke.service.impl.SmartLockServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 优电接口事件处理
 * xiaolong
 * 2023-06-06
 */
@Service
public class SmartLockYdEventHandleService {
    @Autowired
    private SmartLockServiceImpl smartLockService;
    @Autowired
    private SmartLockYdService smartLockYdService;
    @Autowired
    private SmartLockPwdServiceImpl smartLockPwdService;
    @Autowired
    private SmartLockFaceServiceImpl smartLockFaceService;
    @Autowired
    private SmartLockCardServiceImpl smartLockCardService;

    /**
     * 注册设备
     *
     * @param smartLock 智能锁设备信息
     * @return
     */
    public int insertSmartLock(SmartLock smartLock) {
        String stringResultBean = smartLockYdService.insertSmartLock(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String message = (String) JSONObject.parseObject(stringResultBean).get("message");
            throw new ServiceException(message);
        }
        return smartLockService.insertSmartLock(smartLock);
    }

    /**
     * 获取智能锁临时密码权限信息
     *
     * @param sn 智能锁SN
     * @return
     */
    public List<SmartLockPwd> getLockPassword(String sn) {
        //SmartLock smartLock = new SmartLock();
        //smartLock.setSn(sn);
        //String stringResultBean = smartLockYdService.getLockPassword(smartLock);
        //Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        //List<SmartLockDetails> smartLockDetailsList;
        //if (code == 0) {
        //    JSONArray jsonArray = JSONObject.parseObject(stringResultBean).getJSONObject("data").getJSONArray("list");
        //    smartLockDetailsList = jsonArray.toJavaList(SmartLockDetails.class);
        //} else {
        //    throw new RuntimeException("没有该设备信息！");
        //}
        //return smartLockDetailsList;
        return smartLockPwdService.selectSmartLockPwdBySN(sn);

    }

    /**
     * 新增智能锁密码授权信息和智能锁密码授权
     *
     * @param smartLockPwd 智能锁密码授权信息
     * @return 结果
     */
    public int insertSmartLockPwd(SmartLockPwd smartLockPwd) {
        if (StrUtil.isEmpty(smartLockPwd.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (smartLockPwd.getStartTime() == null) {
            throw new ServiceException("开始时间不能为空");
        }
        if (smartLockPwd.getEndTime() == null) {
            throw new ServiceException("结束时间不能为空");
        }
        if (smartLockPwd.getGenerateType() == null) {
            throw new ServiceException("请选择密码生成方式");
        }

        if (SmartLockPwd.PASSWORD_GENERATE_TYPE_SYSGEN.equals(smartLockPwd.getGenerateType())) {
            smartLockPwd.setPassword(RandomNoUtils.getRandomNum(6));
        } else {
            if (StrUtil.isEmpty(smartLockPwd.getPassword())) {
                throw new ServiceException("密码不能为空");
            }
            if (smartLockPwd.getPassword().length() != 6 || !StringUtils.isNumeric(smartLockPwd.getPassword())) {
                throw new ServiceException("请输入6位数字密码");
            }
        }

        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockPwd.getSn());
        smartLock.setPassword(smartLockPwd.getPassword());
        smartLock.setStartTime(smartLockPwd.getStartTime());
        smartLock.setEndTime(smartLockPwd.getEndTime());
        String stringResultBean = smartLockYdService.addPassword(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String message = (String) JSONObject.parseObject(stringResultBean).get("message");
            throw new ServiceException(message);
        }
        Long id = (Long) JSONObject.parseObject(stringResultBean).getJSONObject("data").get("id");
        String password = (String) JSONObject.parseObject(stringResultBean).getJSONObject("data").get("password");
        smartLockPwd.setReturnPwdId(id);
        smartLockPwd.setPassword(password);
        return smartLockPwdService.insertSmartLockPwd(smartLockPwd);
    }


    /**
     * 删除智能锁密码授权信息和删除密码授权
     *
     * @param returnPwdId 厂商授权密码id
     * @return
     */
    public int deleteSmartLockPwd(Long returnPwdId) {
        SmartLock smartLock = new SmartLock();
        smartLock.setReturnPwdId(returnPwdId);
        String stringResultBean = smartLockYdService.deletePwd(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String message = (String) JSONObject.parseObject(stringResultBean).get("message");
            throw new ServiceException(message);
        }
        return smartLockPwdService.deleteSmartLockPwdByReturnPwdId(returnPwdId);
    }

    /**
     * 新增智能锁授权人脸信息
     *
     * @param smartLockFace 智能锁授权人脸信息
     * @return 结果
     */
    public int insertSmartLockFace(SmartLockFace smartLockFace) {
        if (StrUtil.isEmpty(smartLockFace.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (StrUtil.isEmpty(smartLockFace.getFaceImage())) {
            throw new ServiceException("请上传人脸照");
        }

        if (SmartLock.PERMANENT_FLAG_NO.equals(smartLockFace.getPasswordType())) {
            if (smartLockFace.getStartTime() == null) {
                throw new ServiceException("开始时间不能为空");
            }
            if (smartLockFace.getEndTime() == null) {
                throw new ServiceException("结束时间不能为空");
            }
        }


        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockFace.getSn());
        smartLock.setFaceImage(smartLockFace.getFaceImage());
        smartLock.setStartTime(smartLockFace.getStartTime());
        smartLock.setEndTime(smartLockFace.getEndTime());
        smartLock.setPasswordType(smartLockFace.getPasswordType());
        String stringResultBean = smartLockYdService.addFaceImage(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String message = (String) JSONObject.parseObject(stringResultBean).get("message");
            throw new ServiceException(message);
        }
        Long id = (Long) JSONObject.parseObject(stringResultBean).getJSONObject("data").get("id");
        smartLockFace.setReturnFaceId(id);
        return smartLockFaceService.insertSmartLockFace(smartLockFace);

    }

    /**
     * 删除智能锁人脸授权信息和删除人脸授权
     * returnFaceId 厂商授权id
     *
     * @return
     */
    public int deleteSmartLockFace(Long returnFaceId) {
        SmartLock smartLock = new SmartLock();
        smartLock.setReturnFaceId(returnFaceId);
        String stringResultBean = smartLockYdService.deleteFace(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String message = (String) JSONObject.parseObject(stringResultBean).get("message");
            throw new ServiceException(message);
        }
        return smartLockFaceService.deleteSmartLockFaceByReturnFaceId(returnFaceId);
    }

    /**
     * 删除智能锁设备
     */
    @Transactional
    public int deleteSmartLockByLockId(SmartLock smartLock) {
        String sn = smartLock.getSn();
        String stringResultBean = smartLockYdService.deleteAll(sn);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String message = (String) JSONObject.parseObject(stringResultBean).get("message");
            throw new ServiceException(message);
        }
        smartLockCardService.deleteSmartLockCardBySn(smartLock.getSn());
        smartLockFaceService.deleteSmartLockFaceBySn(smartLock.getSn());
        smartLockPwdService.deleteSmartLockPwdBySn(smartLock.getSn());
        return smartLockService.deleteSmartLockByLockId(smartLock.getLockId());
    }

    /**
     * 新增智能锁门卡授权信息和授权门卡
     */
    public int insertSmartLockCard(SmartLockCard smartLockCard) {
        if (StrUtil.isEmpty(smartLockCard.getSn())) {
            throw new ServiceException("智能锁sn号不能为空");
        }
        if (smartLockCard.getStartTime() == null) {
            throw new ServiceException("开始时间不能为空");
        }
        if (smartLockCard.getEndTime() == null) {
            throw new ServiceException("结束时间不能为空");
        }
        if (smartLockCard.getCardType() == null) {
            throw new ServiceException("请选择授权门卡类型");
        }
        if (SmartLockCard.IDENTITY_CARD_TYPE.equals(smartLockCard.getCardType())) {
            if (smartLockCard.getCardNumber().length() != 16) {
                throw new ServiceException("卡号输入错误");
            }
        } else {
            throw new ServiceException("暂无普通门卡授权功能");
            //if (smartLockCard.getCardNumber().length() != 8) {
            //    //throw new RuntimeException("卡号输入错误");
            //}
        }
        SmartLock smartLock = new SmartLock();
        smartLock.setSn(smartLockCard.getSn());
        smartLock.setStartTime(smartLockCard.getStartTime());
        smartLock.setEndTime(smartLockCard.getEndTime());
        smartLock.setCardType(smartLockCard.getCardType());
        smartLock.setCardNumber(smartLockCard.getCardNumber());
        String stringResultBean = smartLockYdService.addCard(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String message = (String) JSONObject.parseObject(stringResultBean).get("message");
            throw new ServiceException(message);
        }
        Long id = (Long) JSONObject.parseObject(stringResultBean).getJSONObject("data").get("id");
        smartLockCard.setReturnCardId(id);
        return smartLockCardService.insertSmartLockCard(smartLockCard);
    }

    /**
     * 获取智能锁门卡信息
     *
     * @param sn 智能锁SN
     * @return
     */
    public List<SmartLockCard> selectLockCard(String sn) {
        return smartLockCardService.selectSmartLockCardBySN(sn);
    }

    /**
     * 删除智能锁门卡授权信息和授权门卡授权
     *
     * @return
     */
    public int deleteSmartLockCard(SmartLockCard smartLockCard) {
        SmartLock smartLock = new SmartLock();
        smartLock.setReturnCardId(smartLockCard.getReturnCardId());
        String stringResultBean = smartLockYdService.deleteCard(smartLock);
        Integer code = (Integer) JSONObject.parseObject(stringResultBean).get("code");
        if (code != 0) {
            String msg = (String) JSONObject.parseObject(stringResultBean).get("msg");
            throw new ServiceException(msg);
        }
        return smartLockCardService.deleteSmartLockCardByReturnCardId(smartLockCard.getReturnCardId());
    }
}
